/**
 * 截取字符串，多余的部分用...代替
 */
export let setString = (str, len) => {
    let StrLen = 0
    let s = ''
    for (let i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 128) {
            StrLen += 2
        } else {
            StrLen++
        }

        s += str.charAt(i)
        if (StrLen >= len) {
            return s + '...'
        }
    }

    return s
}

//格式化设置
export let OptionFormat = (GetOptions) => {
    let options = '{'
    for (let n = 0; n < GetOptions.length; n++) {
        options = options + '\'' + GetOptions[n].option_name + '\':\'' + GetOptions[n].option_value + '\''
        if (n < GetOptions.length - 1) {
            options = options + ','
        }
    }

    return JSON.parse(options + '}')
}

//数组去重
export let HovercUnique = (arr) => {
        let n = {}
        let r = []
        for (var i = 0; i < arr.length; i++) {
            if (!n[arr[i]]) {
                n[arr[i]] = true
                r.push(arr[i])
            }
        }

        return r
    }
    //获取json长度
export let getJsonLength = (jsonData) => {
    var arr = []
    for (var item in jsonData) {
        arr.push(jsonData[item])
    }

    return arr.length
}

export let errCode = {
    ERROR_ADMIN_PASSWORD: { code: 101, msg: '密码输入错误' },
    ERROR_ADMIN_STATUS: { code: 102, msg: '该管理员已经被禁止' },
    ERROR_ADMIN_DELETE: { code: 103, msg: '该管理员已经被删除' },
    ERROR_ADMIN_LEVEL: { code: 104, msg: '管理员权限不足' },
    ERROR_ADMIN_NOTFOUND: { code: 105, msg: '用户不存在' },
    ERROR_ADMIN_EXIST: { code: 106, msg: '用户已存在' },
}
export let uuid = () => {
    function S4() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
export let getSecond = () => {
    return Math.round(new Date().getTime() / 1000)
}
export let getDay = () => {
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    return year * 10000 + month * 100 + day
}
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

function R(number) {
    return chars.charAt(Math.round(Math.random() * number))
}

export let getCode = () => {
    let code = ''
    while(code.length < 6) {
        code += R(9)
    }
    return code
}
export let _getInviteCode = () => {
    let code = ''
    let length = chars.length - 1
    while(code.length < 12) {
        code += R(length)
    }
    return code
}